package com.demo.PathMatchingResourcePatternResolver;

import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.stereotype.Component;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

/**
 * 参考文件 https://www.cnblogs.com/dw3306/p/15690354.html
 * */
@Component
public class PathMatchingResourcePatternResolverDemo {

    public void test1(){
        PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
        //获取的是项目根目录下的文件
        Resource resource = resolver.getResource("file:ReadMe.md");
        print(resource, "file:相对路径");

        //磁盘绝对路径
        Resource resource1 = resolver.getResource("file:D:\\test.js");
        print(resource1, "file:磁盘绝对路径");
    }

    public void test2(){
        PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
        //读取资源目录 resource 下文件
        Resource resource = resolver.getResource("application.properties");
        print(resource, "无前缀 resource");

        //如果不是根目录，也可以指定目录
        Resource resource1 = resolver.getResource("file/pz.txt");
        print(resource1, "无前缀带目录 resource");

        //也可以明确指定 classpath
        Resource resource2 = resolver.getResource("classpath:file/pz.txt");
        print(resource2, "classpath 带目录 resource");

        //这里是获取编译后的
        Resource resource3 = resolver.getResource("com/demo/interfaces/aware/Dog.class");
        print(resource3, "class 文件");
    }

    public void test3() throws IOException {
        PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
        //读取资源目录 resource 下文件
        Resource[] resource = resolver.getResources("classpath*:META-INF/spring.factories");
        for(Resource resource1:resource) {
            print(resource1, "classpath* resource");
        }
    }

    /**
     * 得配合 classpath*
     * */
    public void test4() throws IOException {
        PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();

        //文件名匹配
        Resource[] resources = resolver.getResources("classpath*:file/p*.txt");
        for(Resource resource : resources) {
            print(resource, "文件名通配符");
        }

        //单个路径匹配 * 匹配一层目录
        Resource[] resources1 = resolver.getResources("classpath*:*/pz.txt");
        for(Resource resource : resources1) {
            print(resource, "单个路径通配符");
        }

        //多个路径匹配 ** 匹配多层目录
        Resource[] resources2 = resolver.getResources("classpath*:**/pz.txt");
        for(Resource resource : resources2) {
            print(resource, "多个路径通配符");
        }
    }

    private void print(Resource resource, String g){
        System.out.println("-----------------"+ g +"---------------");
        try(BufferedReader br = new BufferedReader(new InputStreamReader(resource.getInputStream()))){
            String s;
            do{
                s = br.readLine();
                System.out.println(s);
            }while (s != null);
        }catch (IOException e){
            System.out.println("异常");
        }
    }
}
